package org.ysu.service.impl;

import org.springframework.stereotype.Service;
import org.ysu.dto.RegionData;
import org.ysu.dto.RegionDataforBigDecimal;
import org.ysu.service.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
@Service
public class EChartsDataServiceImpl implements EChartsDataService {
    @Resource
    StatisticsService statisticsService;
    @Resource
    ProvincesService provincesService;
    @Resource
    CitiesService citiesService;
    @Resource
    InterviewService interviewService;
    @Override
    public Map<String, Object> getFormattedRegionDataOfAvgIncome(String regionCode) {
        // 获取原始数据
        List<RegionDataforBigDecimal> regionDataList = statisticsService.getRegionAvgIncome(regionCode);

        // 初始化结果的 categories 和 data
        List<String> categories = new ArrayList<>();
        List<BigDecimal> data = new ArrayList<>();

        // 处理数据
        for (RegionDataforBigDecimal regionData : regionDataList) {
            categories.add(regionData.getName());
            data.add(regionData.getValue());
        }

        String name;
        if (Objects.equals(regionCode, "0")){
            name="全国";
        } else if (regionCode.length()==2) {
            name=provincesService.getNameById(Long.valueOf(regionCode));
        } else if (regionCode.length()==4) {
            name=citiesService.getNameById(Long.valueOf(regionCode));
        }else {
            name="";
        }

        String level;
        if(regionCode.length()==1){
            level="省级";
        } else if (regionCode.length()==2) {
            level="市级";
        } else if (regionCode.length()==4) {
            level="县（区）级";
        }else{
            level="";
        }

        // 构建 JSON 格式的 Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("title", (name+level+"行政区贫困户年平均收入"));
        resultMap.put("legend", Arrays.asList("年收入平均值/元"));
        resultMap.put("categories", categories);
        resultMap.put("data", data);

        return resultMap;
    }
    public Map<String,Object> getFormattedRegionDataOfCount(String regionCode){
        List<RegionData> regionDataList = statisticsService.getRegionCount(regionCode);
        String rId;
        if(regionCode.length()==1){
            rId="0";
        } else if (regionCode.length()==2) {
            rId=regionCode+"0000";
        } else if (regionCode.length()==4) {
            rId=regionCode+"00";
        }else{
            rId="";
        }

        String name;
        if (Objects.equals(regionCode, "0")){
            name="全国";
        } else if (regionCode.length()==2) {
            name=provincesService.getNameById(Long.valueOf(regionCode));
        } else if (regionCode.length()==4) {
            name=citiesService.getNameById(Long.valueOf(regionCode));
        }else {
            name="";
        }

        String level;
        if(regionCode.length()==1){
            level="省级";
        } else if (regionCode.length()==2) {
            level="市级";
        } else if (regionCode.length()==4) {
            level="县（区）级";
        }else{
            level="";
        }

        int max=0;
        for(RegionData r: regionDataList){
            if(r.getValue()>max){
                max=r.getValue();
            }
        }

        // 构建 JSON 格式的 Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("title",(name+level+"行政区贫困户总数地区分布"));
        resultMap.put("legend","贫困户/户");
        resultMap.put("map",rId);
        resultMap.put("datamax",max);
        resultMap.put("data",regionDataList);
        return resultMap;
    }

    public Map<String,Object> getDailyInterviewCountsOfLastSevenDays(String regionCode, LocalDate today){
        long rId = Long.parseLong(regionCode);
        LocalDate startDate = today.minusDays(7);
        List<Integer> data = interviewService.getDailyInterviewCounts(startDate,today,rId);
        List<String> date = new ArrayList<>();
        for(int i=0;i<7;i++){
            date.add(today.minusDays((7-i)).toString());
        }
        Map<String,Object> result = new HashMap<>();
        result.put("data",data);
        result.put("categories",date);
        return result;
    }

    public List<List<Object>> getAdminInterviewSummaries(String regionCode,LocalDate startDay,LocalDate endDay){
        long rId = Long.parseLong(regionCode);
        return interviewService.getAdminInterviewSummaries(rId,startDay,endDay);
    }
}
